package com.hisense.hiioc.hismartplate.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hisense.hiioc.hismartplate.base.BaseController;
import com.hisense.hiioc.hismartplate.constants.URLConstant;
import com.hisense.hiioc.hismartplate.entity.response.BaseRes;
import com.hisense.hiioc.hismartplate.entity.table.DeviceSnapEntity;
import com.hisense.hiioc.hismartplate.service.DeviceScreenSnapService;
import com.hisense.hiioc.hismartplate.utils.EmptyUtils;
import com.hisense.hiioc.hismartplate.utils.ServerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.net.InetAddress;
import java.util.List;
import java.util.Map;

/**
 * @Auther:liaohongbing@hisense.com
 * @date:2019/1/2 11:47
 * @des
 */

@Controller
@RequestMapping(URLConstant.SnapMonitorURLMapping.BASE_URL)
public class SnapMonitorController extends BaseController<DeviceScreenSnapService> {

    @Autowired
    ServerUtil serverUtil;

    @RequestMapping("index")
    public String snapMonitorHome() {
        return "snapMonitor/snapHome";
    }

    @RequestMapping("home")
    public String test(Model model, @RequestBody(required = false) Map<String, Object> queryMap) {
        //System.out.println(serverUtil.getBaseUrl());
        int pageNum = 1, pageSize = 5;
        System.out.println("query map:"+queryMap);
        if (queryMap != null && queryMap.get("pageNum") != null && queryMap.get("pageSize") != null) {
            pageNum = Integer.parseInt((String) queryMap.get("pageNum"));
            pageSize = Integer.parseInt((String) queryMap.get("pageSize"));
        }
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Map> deviceList = defaultService.getDeviceList(queryMap);
        for (Map device : deviceList) {
            String deviceId = device.get("DEVICE_ID").toString();
            List<DeviceSnapEntity> snapList = defaultService.getDeviceSnap(deviceId);
            for (DeviceSnapEntity entity : snapList) {
                File snapFile = new File(entity.getSnapPath());
                entity.setSnapPath("http://" + serverUtil.getServer() + ":" + serverUtil.getPort() + "/snapFile/" + snapFile.getName());
            }
            device.put("snapList", snapList);
        }
        model.addAttribute("deviceList", deviceList);
        model.addAttribute("pageSize", page.getPageSize());
        model.addAttribute("pageNum", page.getPageNum());
        model.addAttribute("total", page.getTotal());
        model.addAttribute("pages", page.getPages());
        Exception e =null;
        
        return "snapMonitor/home";
    }


    @GetMapping("refreshHome")
    public String refreshDevSnapList(Model model, @RequestParam(required = false) Map queryMap) {
        System.out.println(queryMap);
        int pageNum = 1, pageSize = 5;
        System.out.println(queryMap);
        if (queryMap != null && queryMap.get("pageNum") != null && queryMap.get("pageSize") != null) {
            pageNum = Integer.parseInt((String) queryMap.get("pageNum"));
            pageSize = Integer.parseInt((String) queryMap.get("pageSize"));
        }
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Map> deviceList = defaultService.getDeviceList(queryMap);
        for (Map device : deviceList) {
            String deviceId = device.get("DEVICE_ID").toString();
            List<DeviceSnapEntity> snapList = defaultService.getDeviceSnap(deviceId);
            for (DeviceSnapEntity entity : snapList) {
                File snapFile = new File(entity.getSnapPath());
                entity.setSnapPath(serverUtil.getBaseUrl() + "/snapFile/" + snapFile.getName());
            }
            device.put("snapList", snapList);
        }

        model.addAttribute("deviceList", deviceList);
        model.addAttribute("pageSize", page.getPageSize());
        model.addAttribute("pageNum", page.getPageNum());
        model.addAttribute("total", page.getTotal());
        model.addAttribute("pages", page.getPages());

        return "snapMonitor/home::snapTable";
    }

    @GetMapping("list")
    @ResponseBody
    public BaseRes listDevSnapList(@RequestParam(required = false) Map queryMap) {
        System.out.println(queryMap);
        int pageNum = 1, pageSize = 5;
        System.out.println(queryMap);
        if (queryMap != null && queryMap.get("pageNum") != null && queryMap.get("pageSize") != null) {
            pageNum = Integer.parseInt((String) queryMap.get("pageNum"));
            pageSize = Integer.parseInt((String) queryMap.get("pageSize"));
        }
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Map> deviceList = defaultService.getDeviceList(queryMap);
        for (Map device : deviceList) {
            String deviceId = device.get("DEVICE_ID").toString();
            List<DeviceSnapEntity> snapList = defaultService.getDeviceSnap(deviceId);
            for (DeviceSnapEntity entity : snapList) {
                File snapFile = new File(entity.getSnapPath());
                entity.setSnapPath(serverUtil.getBaseUrl() + "/snapFile/" + snapFile.getName());
            }
            device.put("snapList", snapList);
        }
        JSONObject result = new JSONObject();
        result.put("deviceList",deviceList);
        result.put("pageSize",page.getPageSize());
        result.put("pageNum",page.getPageNum());
        result.put("total",page.getTotal());
        result.put("pages",page.getPages());
        return generateHttpOkRes(result);
    }

    @ResponseBody
    @GetMapping("getSnapInfo")
    public BaseRes getDeviceSnapInfo(@RequestParam("deviceId") String deviceId) {
        List<DeviceSnapEntity> snapEntityList = defaultService.getDeviceSnap(deviceId);
        //String snapPath = snapEntity.getSnapPath();
        if (EmptyUtils.isEmpty(snapEntityList)) {
            return generateHttpBadRes(500, "查询为空");
        }
        String host = "";

        for (DeviceSnapEntity entity : snapEntityList) {
            File snapFile = new File(entity.getSnapPath());
            try {
                host = InetAddress.getLocalHost().getHostAddress();
                System.out.println(host);
                entity.setSnapPath("http://localhost:11186" + "/snapFile/" + snapFile.getName());
            } catch (Exception e) {
            }
        }
        return generateHttpOkRes(snapEntityList);
    }


}
